// store/user.js代码如下
import {defineStore} from 'pinia'
import {
    calculateValidTime,
    handleLocalStorage,
} from "@/utils/token.js";
import {showToast} from "vant";

const useUser = defineStore('user', {
    state: () => ({
        activityRulesChecked: false, // 是否勾选活动规则
        privacyPolicyChecked: false, // 是否勾选隐私政策
        haveReadState: false,  //是否是已读状态
        userPhone: '',  //手机号码
        userCaptcha: '', //验证码
        buttonMsg: '获取验证码', //验证码提示语
        flag: false, //获取验证码的状态
        waitTime: 60, // 获取验证码按钮失效时间
        access_token: '', // token
        token_type: '', // token状态
        expires_in: '', // token有效时间
        refresh_token: '', // token刷新命令
        countDownTime: '', // 订单倒计时
    }),
    /**
     * 用来封装计算属性 有缓存功能  类似于computed
     */
    getters: {},
    /**
     * 编辑业务逻辑  类似于methods
     */
    actions: {
        //存储token
        handleTokenChange(data) {
            // alert("获取到token的对象值:" + JSON.stringify(data))
            this.access_token = data.access_token
            this.expires_in = data.expires_in
            this.token_type = data.token_type
            this.refresh_token = data.refresh_token
            handleLocalStorage(data)
        },
        // 授权弹窗-授权勾选状态
        handleHaveRead() {
            this.haveReadState = this.activityRulesChecked && this.privacyPolicyChecked
        },
        //将授权的状态存储本地
        handleLocalStorageAccreditState() {
            localStorage.setItem('haveReadState', this.haveReadState)
            localStorage.setItem('activityRulesChecked', this.activityRulesChecked)
            localStorage.setItem('privacyPolicyChecked', this.privacyPolicyChecked)
        },
        //获取验证码倒计时
        handleCountDownChange() {
            this.flag = true
            this.waitTime--
            this.buttonMsg = `${this.waitTime} 秒后重新发送`
            let timer = setInterval(() => {
                if (this.waitTime > 1) {
                    this.flag = true
                    this.waitTime--
                    this.buttonMsg = `${this.waitTime}秒后重新获取`
                } else {
                    clearInterval(timer)
                    this.buttonMsg = '重新获取'
                    this.flag = false
                    this.waitTime = 61
                }
            }, 1000)
        },
        //倒计时
        countdown(creationTime) {
            let targetTime = Math.round(new Date(creationTime) / 1000) //创建时间
            let remainingMinutes = 0
            let remainingSeconds = 0
            let countdownInterval = null
            let targetTimeValue = targetTime + 600; //有效时间
            let currentTime = Math.round(new Date() / 1000); //当前时间
            let timeDiff = targetTimeValue - currentTime;
            if (timeDiff > 600) {
                showToast('倒计时结束');
                this.countDownTime = `0${remainingMinutes} : 0${remainingSeconds}`
            } else {
                countdownInterval = setInterval(() => {
                    timeDiff -= 1
                    // console.log(timeDiff, 666)
                    if (timeDiff <= 0) {
                        clearInterval(countdownInterval);
                        showToast('倒计时结束');
                    } else {
                        remainingMinutes = Math.floor((timeDiff % 3600 / 60))
                        remainingSeconds = Math.floor((timeDiff % 60))
                        this.countDownTime = `0${remainingMinutes} : ${remainingSeconds}`
                    }
                }, 1000);
            }
        }
    }
})
export default useUser
